//
//  LeftDrawerViewController.m
//  STL_ Animation
//
//  Created by 孙健 on 16/3/30.
//  Copyright © 2016年 STL. All rights reserved.
//

#import "LeftDrawerViewController.h"
#import "AppDelegate.h"
#import "LeftTableViewCell.h"
#import "HomePageViewController.h"
#import "ChannelViewController.h"
#import "VSaleViewController.h"
#import "YSingleViewController.h"

#import "LoadViewController.h"
#import "CollectViewController.h"
#import "HistoryViewController.h"
#import "AboutMeViewController.h"
#import "CleanCaches.h"
@interface LeftDrawerViewController ()<UITableViewDelegate,UITableViewDataSource,UIImagePickerControllerDelegate,UINavigationControllerDelegate>

@property (nonatomic, strong) UITableView *tableview;
@property (nonatomic, strong) UIImageView *headImageView;
@property (nonatomic, strong) NSString *path;

@end

@implementation LeftDrawerViewController

- (void)viewDidLoad {
    [super viewDidLoad];

//    8 146 114
    self.view.backgroundColor = [UIColor colorWithRed:8/255.0 green:146/255.0 blue:114/255.0 alpha:1];
    
    self.tableview = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    self.tableview.dataSource = self;
    self.tableview.delegate  = self;
    self.tableview.scrollEnabled = NO; //设置tableview 不能滚动
    self.tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    UINib *nib = [UINib nibWithNibName:@"LeftTableViewCell" bundle:nil];
    [self.tableview registerNib:nib forCellReuseIdentifier:@"cell"];
    
    [self.view addSubview:self.tableview];
}

- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *path = [paths lastObject];
    NSString *pathLast = [path stringByAppendingString:@"/person.plist"];
    self.path = pathLast;
    if ([NSKeyedUnarchiver unarchiveObjectWithFile:self.path]) {
        
        
        self.headImageView.image = [UIImage imageWithData:[NSKeyedUnarchiver unarchiveObjectWithFile:self.path]];
//        NSLog(@"-----%@-----", self.path);
    }

}




- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        
        return 4;
    }else {
        
        return 5;
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 2;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

//    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    
    LeftTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    cell.backgroundColor = [UIColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:0.5];
    cell.leftLabel.font = [UIFont systemFontOfSize:14];
    cell.leftLabel.textColor = [UIColor whiteColor];
   
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
   
    if (indexPath.section == 0) {
       
        if (indexPath.row == 0) {
            cell.leftLabel.text = @"首页";
            cell.leftImage.image = [[UIImage imageNamed:@"homepage"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        } else if (indexPath.row == 1) {
            cell.leftLabel.text = @"频道";
            cell.leftImage.image = [[UIImage imageNamed:@"channelpage"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        } else if (indexPath.row == 2) {
            cell.leftLabel.text = @"V榜";
            cell.leftImage.image = [[UIImage imageNamed:@"v3"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        } else if (indexPath.row == 3) {
            cell.leftLabel.text = @"悦单";
            cell.leftImage.image = [[UIImage imageNamed:@"ysinglepage"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
            
        }
        
    }
    
    if (indexPath.section == 1) {
        
       
         if (indexPath.row == 0){
            cell.leftLabel.text = @"下载管理";
            cell.leftImage.image = [[UIImage imageNamed:@"xiazai"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        }
        else if (indexPath.row == 1) {
            
            cell.leftLabel.text = @"收藏列表";
            cell.leftImage.image = [[UIImage imageNamed:@"shoucang"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
            
        }
        else if (indexPath.row == 2) {
            
            cell.leftLabel.text = @"播放历史";
            cell.leftImage.image = [[UIImage imageNamed:@"lishi"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
            
        }
        else if (indexPath.row == 3) {
            
            cell.leftLabel.text = @"清理缓存";
            cell.leftImage.image = [[UIImage imageNamed:@"huancun"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
            
        }
        
        else if (indexPath.row == 4) {
            
            cell.leftLabel.text = @"关于我们";
            cell.leftImage.image = [[UIImage imageNamed:@"guanyu"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
            
        }


    }
    
    
    
    
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
     AppDelegate *tempAppDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    
    if (indexPath.section == 0) {
        
        if (indexPath.row == 0) {
            HomePageViewController *homePage = [[HomePageViewController alloc] init];
            [tempAppDelegate.LeftSlideVC closeLeftView];//关闭左侧抽屉
            
            [tempAppDelegate.homePageNavigation pushViewController:homePage animated:YES];
            
        }
        else if (indexPath.row == 1) {
            
            ChannelViewController *channelVC = [[ChannelViewController alloc] init];
            [tempAppDelegate.LeftSlideVC closeLeftView];//关闭左侧抽屉
            [tempAppDelegate.homePageNavigation pushViewController:channelVC animated:YES];
            
        }
        else if (indexPath.row == 2) {
            
            VSaleViewController *vsaleVC = [[VSaleViewController alloc] init];
            [tempAppDelegate.LeftSlideVC closeLeftView];//关闭左侧抽屉
            [tempAppDelegate.homePageNavigation pushViewController:vsaleVC animated:YES];
            
        } else if (indexPath.row == 3) {
            
            YSingleViewController *ysingleVC = [[YSingleViewController alloc] init];
            [tempAppDelegate.LeftSlideVC closeLeftView];//关闭左侧抽屉
            [tempAppDelegate.homePageNavigation pushViewController:ysingleVC animated:YES];
            
        }
 }
    
    else if (indexPath.section == 1) {
        
        if (indexPath.row == 0) {
            LoadViewController *loadVc = [[LoadViewController alloc] init];
            [tempAppDelegate.LeftSlideVC closeLeftView];//关闭左侧抽屉
            [tempAppDelegate.homePageNavigation pushViewController:loadVc animated:YES];
            
        }
        else if (indexPath.row == 1) {
            
            CollectViewController *collectVc = [[CollectViewController alloc] init];
            [tempAppDelegate.LeftSlideVC closeLeftView];//关闭左侧抽屉
            [tempAppDelegate.homePageNavigation pushViewController:collectVc animated:YES];
            
        }
        else if (indexPath.row == 2) {
            
            HistoryViewController *historyVc = [[HistoryViewController alloc] init];
            [tempAppDelegate.LeftSlideVC closeLeftView];//关闭左侧抽屉
            [tempAppDelegate.homePageNavigation pushViewController:historyVc animated:YES];
            
        } else if (indexPath.row == 3) {
            
            UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"清理缓存" message:[NSString stringWithFormat:@"当前缓存为%@,是否清理缓存？", [CleanCaches folderSizeAtPath]] preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *sure = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                [CleanCaches cleanCachesAtPath];
            }];
            UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
            [alertController addAction:sure];
            [alertController addAction:cancel];
            
            [self presentViewController:alertController animated:YES completion:nil];
            
        } else if (indexPath.row == 4) {
            
            AboutMeViewController *aboutMeVc = [[AboutMeViewController alloc] init];
            [tempAppDelegate.LeftSlideVC closeLeftView];//关闭左侧抽屉
            [tempAppDelegate.homePageNavigation pushViewController:aboutMeVc animated:YES];
            
        }

        
    }
    
   
    
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        
        return [UIScreen mainScreen].bounds.size.height / 6;
    }
    
    else {
        
        return 10;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return [UIScreen mainScreen].bounds.size.height / 13;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    
    if (section == 0) {
        
      UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.tableview.bounds.size.width, [UIScreen mainScreen].bounds.size.height/6)];
      view.backgroundColor = [UIColor clearColor];
        //头像
       self.headImageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, view.bounds.size.height / 10 * 3, view.bounds.size.height / 5 * 3, view.bounds.size.height / 5 * 3)];
        [view addSubview:self.headImageView];
        self.headImageView.userInteractionEnabled = YES;
        self.headImageView.image = [UIImage imageNamed:@"homePlace1"];
        [self.headImageView.layer setCornerRadius:view.bounds.size.height / 10 * 3];
        self.headImageView.layer.masksToBounds = YES;
        //    然后再给图层添加一个有色的边框
        self.headImageView.layer.borderWidth = 3;
        self.headImageView.layer.borderColor = [[UIColor colorWithRed:69/255.0 green:158/255.0 blue:116/255.0 alpha:1] CGColor];
        
        
        //头像按钮
        UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
        button.frame = self.headImageView.frame;
        [view addSubview:button];
        [button addTarget:self action:@selector(getHeadImage) forControlEvents:UIControlEventTouchUpInside];
        
        
        //logo图（哇哇必）
        UIImageView *logoImage = [[UIImageView alloc] initWithFrame:CGRectMake(view.bounds.size.height, view.bounds.size.height / 20 * 9, view.bounds.size.height, view.bounds.size.height / 10 * 3)];
        logoImage.image = [[UIImage imageNamed:@"logoimage"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        logoImage.contentMode = UIViewContentModeScaleAspectFit;
        
        [view addSubview:logoImage];

         return view;
    }
   
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.tableview.bounds.size.width, 10)];
    view.backgroundColor = [UIColor colorWithRed:84/255.0 green:90/255.0 blue:78/255.0 alpha:0.6];
    return view;
}


- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info {
    
    [picker dismissViewControllerAnimated:YES completion:^{
        
    }];
    
    UIImage *image = info[@"UIImagePickerControllerEditedImage"];
    
    self.headImageView.image = image;
    
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *path = [paths lastObject];
    NSString *pathLast = [path stringByAppendingString:@"/person.plist"];
    self.path = pathLast;
    NSData *data = nil;
    if (UIImagePNGRepresentation(image)) {
        
        data = UIImagePNGRepresentation(image);
    } else {
        
        data = UIImageJPEGRepresentation(image, 1);
    }
    
    [NSKeyedArchiver archiveRootObject:data toFile:path];
//    NSLog(@"%@", self.path);
//    NSLog(@"%@", data);
}



- (void)getHeadImage {
   
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    
    picker.delegate = self;
    picker.allowsEditing = YES;
    picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    
    [self presentViewController:picker animated:YES completion:^{
        
    }];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
